/*
 *   MetrixViewer - A simple app for viewer Metrix data. Uses XML data from Webosroundup's Metrix data and graphs it using Google charts API.
 *   http://code.google.com/p/metrixviewer/
 *   Copyright (C) 2010  Wes Brown
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
function AddAppAssistant() {

}

AddAppAssistant.prototype.setup = function() {

	//Widgets
	this.appnameTextModel = {
             value: "",
             disabled: false
    };
	this.controller.setupWidget("appnameFieldId",
        {
            hintText: $L(""),
            multiline: false,
            enterSubmits: false,
            focus: false,
			textCase: Mojo.Widget.steModeLowerCase
         },
         this.appnameTextModel
    ); 
	
	
	this.packageTextModel = {
             value: "",
             disabled: false
    };
	this.controller.setupWidget("packageTextFieldId",
        {
            hintText: $L("e.g com.domain.app"),
            multiline: false,
            enterSubmits: false,
            focus: false,
			textCase: Mojo.Widget.steModeLowerCase
         },
         this.packageTextModel
    ); 
	
	this.controller.setupWidget("submitButtonId",
         {},
         {
             label : $L("Submit"),
             disabled: false
         }
     );
	
	Mojo.Event.listen(this.controller.get("submitButtonId"),Mojo.Event.tap, this.submitNew.bind(this));

};

AddAppAssistant.prototype.activate = function(event) {
	/* put in event handlers here that should only be in effect when this scene is active. For
	   example, key handlers that are observing the document */
};

AddAppAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	   this scene is popped or another scene is pushed on top */
};

AddAppAssistant.prototype.cleanup = function(event) {
	/* this function should do any cleanup needed before the scene is destroyed as 
	   a result of being popped off the scene stack */
};




AddAppAssistant.prototype.submitNew = function(event) {

	//Returns data to app selector scene
	var newHost = {
		'appname': this.appnameTextModel.value,
		'packageId': this.packageTextModel.value
	};
	
	
	Mojo.Log.info("New app is %j", newHost);
	
	MetrixViewer.appsCookieObject.push(newHost);
	MetrixViewer.appsCookie.put(MetrixViewer.appsCookieObject);
	 

	
	//Return to app selector
	Mojo.Controller.stageController.popScene();

};